package com.henry.librarymng;

import java.awt.print.Book;
import java.util.ArrayList;

public class LibraryDemo {
    static void printAvailableBooks(Library library) {
        ArrayList<BookEntry> books = library.availableBooks();
        for (BookEntry book : books) { System.out.println(book); }
        System.out.println();
    }

    static void tryBorrowBook(String readerID, String bookName, Library library) {
        try {
            System.out.println(readerID + " tries to borrow " + bookName + " from " + library.getLibraryName() + ".");
            BookEntry borrowed = library.borrow(readerID, bookName);
            System.out.println(readerID + " borrowed " + borrowed.getBookTitle() + " from " + library.getLibraryName() + ".");
        } catch (Exception exception) {
            System.out.println(exception.toString());
        }
        System.out.println();
    }

    static void tryReturnBook(String readerID, String bookName, Library library) {
        try {
            System.out.println(readerID + " tries to return " + bookName + " to " + library.getLibraryName() + ".");
            BookEntry borrowed = library.returnBook(readerID, bookName);
            System.out.println(readerID + " returned " + borrowed.getBookTitle() + " to " + library.getLibraryName() + ".");
        } catch (Exception exception) {
            System.out.println(exception.toString());
        }
        System.out.println();
    }

    public static void main(String[] args) throws Exception {
        Library.setOpeningTime("Opened form 9 a.m. to 5 p.m.");

        Library someLibrary = new Library("Some Library", "10 High St.");
        Library anotherLibrary = new Library("Another Library", "21 2nd Avenue");

        // Print Library Information:
        System.out.println(someLibrary.getLibraryInfo());
        System.out.println("---------------");
        System.out.println(anotherLibrary.getLibraryInfo());
        System.out.println();

        // Adding Books To Libraries:
        someLibrary.addBook("The Da Vinci Code");
        someLibrary.addBook("Le Petit Prince");
        someLibrary.addBook("A Tale of Two Cities");
        someLibrary.addBook("The Lord of the Rings");
        someLibrary.addBook("Cloud Atlas");

        System.out.println("5 books added to Some Library: ");

        printAvailableBooks(someLibrary);

        anotherLibrary.addBook("Cloud Atlas");

        // Try Borrowing and Returning Books:
        tryBorrowBook("Jack_23", "Le Petit Prince", someLibrary);

        System.out.println(someLibrary.getLibraryName() + " now has these books: ");
        printAvailableBooks(someLibrary);

        // Susan try borrowing a book, but the library has no available copies.
        tryBorrowBook("Susan_16", "Le Petit Prince", someLibrary);
        // So susan went to another library. But this library didn't have this book:
        tryBorrowBook("Susan_16", "Le Petit Prince", anotherLibrary);
        // Eventually susan borrowed another book:
        tryBorrowBook("Susan_16", "Cloud Atlas", anotherLibrary);

        // Jack returns book to the correct library:
        tryReturnBook("Jack_23", "Le Petit Prince", someLibrary);
        // After Jack returned his book, Susan tries to borrow it:
        tryBorrowBook("Susan_16", "Le Petit Prince", someLibrary);

        // Susan tries returning a book to a wrong library:
        tryReturnBook("Susan_16", "Cloud Atlas", someLibrary);

        // Susan correctly returning her borrowed books:
        tryReturnBook("Susan_16", "Le Petit Prince", someLibrary);
        tryReturnBook("Susan_16", "Cloud Atlas", anotherLibrary);
    }
}
